package cn.lsnu.modules.watch.controller;

import cn.lsnu.common.interfacetools.interfacecontroller.BaseController;
import cn.lsnu.common.interfacetools.interfaceentity.Page;
import cn.lsnu.common.sys.entity.MenuEntity;
import cn.lsnu.common.sys.service.MenuService;
import cn.lsnu.modules.watch.entity.WatchTaskEntity;
import cn.lsnu.modules.watch.service.WatchTaskService;
import cn.lsnu.utils.Global;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping({"/app/watchtask","/task/watch"})
public class WatchTaskController extends BaseController{


    @Autowired
    private WatchTaskService watchTaskService;

    @Autowired
    private MenuService menuService;

    @RequestMapping({"/page","/listjson"})
    public
    @ResponseBody Page<WatchTaskEntity> pages(WatchTaskEntity page)throws  Exception{
        Page<WatchTaskEntity> pageList = watchTaskService.findPage(page);
        return pageList;
    }

    @RequestMapping({"/lists"})
    public
    @ResponseBody List<WatchTaskEntity> lists(WatchTaskEntity entity)throws  Exception{
        List<WatchTaskEntity> lists = watchTaskService.findList(entity);
        return lists;
    }




    @RequestMapping("/taskdetil")
    public
    @ResponseBody WatchTaskEntity taskDetil(String key)throws  Exception{
        return watchTaskService.get(key);
    }

    @RequestMapping("/list")
    public String listInfos(Model model, HttpSession session, MenuEntity entity)throws Exception{
        String firstTitle = entity.getFirstTitle();
        //每个-list页面的id
        model.addAttribute("pageId",entity.getMenuId());
        //获取页面的所有信息
        String userCode = (String) session.getAttribute(Global.SESSION_ID);
        entity.setUserCode(userCode);
        entity = menuService.getPageMenu(entity);
        entity.setFirstTitle(firstTitle);
        entity.setSecondTitle("信息管理列表");
        model.addAttribute("pageMenuMsg",entity);
        return "webpage/modules/task/watchrecord-list";
    }

    @RequestMapping("/save")
    public void save(WatchTaskEntity entity)throws Exception{
        watchTaskService.save(entity);
    }

    @RequestMapping("/updmsg")
    public String updMsg(String key,Model model)throws Exception{
        WatchTaskEntity entity = watchTaskService.get(key);
        model.addAttribute("updwatch",entity);
        return "webpage/modules/task/watchrecord-upd";
    }

    @RequestMapping("/upd")
    public void upd(WatchTaskEntity entity)throws Exception{
        watchTaskService.update(entity);
    }

    @RequestMapping(value = "/del",method=RequestMethod.POST)
    public
        @ResponseBody WatchTaskEntity
        delete(WatchTaskEntity entity)throws Exception{
        watchTaskService.delete(entity);
        entity.setCode(Global.STATUE_SUCCESS);
        return entity;
    }

    @RequestMapping("/addCount")
    public @ResponseBody
       List<WatchTaskEntity> addCount(WatchTaskEntity entity)throws Exception{
        return watchTaskService.addressCount(entity);
    }

}
